package org.demo.controller;

import org.demo.common.Result;
import org.demo.entity.*;
import org.demo.mapper.ContractMapper;
import org.demo.mapper.HousesMapper;
import org.demo.service.ICommunityService;
import org.demo.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

//合同表
@RestController
@RequestMapping("/contract")
public class ContractConteoller {
    @Autowired
    private IContractService contractService;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private HousesMapper housesMapper;
    @PostMapping("/add")
    public Result add(@RequestBody ContractDto contract){
        if (contract.getIntAmount()==null){
            return Result.error("缺少必要的参数");
        }
        Houses houses = housesMapper.selectById(contract.getHouseId());
        if (houses.getType().equals("1")){
            if (contract.getYuefen()==0){
                return Result.error("请输入合同月份");
            }
            if (contract.getYuefen()<0 || contract.getYuefen()>12){
                return Result.error("合同月份输入不合法");
            }
        }

        if (!(contract.getIntAmount().compareTo(BigDecimal.ZERO) > 0)){
            return Result.error("金额为负数");
        }

        contract.setLandlordId(houses.getLandlordId());
        Contract contract1 = contractMapper.selectChong(contract);
        if (contract1==null){
            contractService.add(contract);
        }else {
            return Result.error("重复添加合同");
        }

        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<Contract>> list(@RequestBody (required = false) Contract contract){
        List<Contract> list= contractService.list(contract);
        return Result.success(list);
    }
    @PostMapping("/Financiallist")
    public Result<List<ContractDto>> Financiallist(@RequestBody (required = false) Contract contract){
        List<ContractDto> list= contractService.Financiallist(contract);
        return Result.success(list);
    }
    @GetMapping("/Pagelist")
    public Result<PageBean<ContractDto>> Pagelist(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @ModelAttribute Contract contract){
        PageBean<ContractDto> pb= contractService.Pagelist(pageNum,pageSize,contract);
        return Result.success(pb);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Contract contract){
        Contract contract1 = contractMapper.selectById(contract.getId());
        if (contract1.getStatus().equals("0")){
            contract1.setStatus("1");
            contractService.update(contract1);
        }else if (contract1.getStatus().equals("2")){
            return Result.error("合同已经失效");
        }else if (contract1.getStatus().equals("1")){
            return Result.error("合同已经生效");
        }

        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public  Result delete(@PathVariable String id){
        contractService.delete(id);
        return Result.success();
    }

}
